package com.situ.mall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.mapper.MemberMapper;
import com.situ.mall.common.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.util.StringUtil;
import com.situ.mall.common.vo.LayuiTableVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author membergpf
 * @since 2023-03-21
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Override
    public LayuiTableVO list(String keyword, Integer page, Integer limit) {
        LayuiTableVO vo =new LayuiTableVO();

        //查询之前  ，分页工具介入
        PageHelper.startPage(page,limit);   //加入startPage方法后，后面的查询就开启分页查，自动加limit
        //startpage是一次性的   startpage方法后第一次查询是分页
        List<Member> list = baseMapper.selectListPage(keyword);
        vo.setData(list);
        return vo;
    }

    @Override
    public void on(Integer id) {
        baseMapper.on(id);
    }

    @Override
    public void off(Integer id) {
        baseMapper.off(id);
    }

    @Override
    public Boolean check(String username, String password) {
        List<Member> l = new ArrayList<>();

        l = baseMapper.selectByUsername(username);
        Boolean b = l.size() > 0 && l.get(0).getPassword().equals(StringUtil.md5(password));

        if (b) {
            RequestUtil.setCurrent(l.get(0));
        }
        return b;
    }

    @Override
    public Member selectByUsername(String username) {
        return baseMapper.selectByUsername2(username);
    }
}
